package com.ss.android.article.base.feature.detail2.a.c;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ss.android.article.base.feature.detail.model.MixedAdsData;
import com.ss.android.article.base.feature.detail.model.PhoneAdsData;
import com.ss.android.article.base.feature.detail2.a.b.a;
import com.ss.android.article.base.feature.detail2.widget.a.BaseDetailLayout;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.base.ui.EllipsisTextView;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.NightModeAsyncImageView;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.image.Image;

import org.json.JSONException;
import org.json.JSONObject;

public final class DetailAdSmallPicLayout extends BaseDetailLayout implements z {
    TextView a;
    String b;
    TextView c;
    long d;
    String e;
    a f;
    JSONObject g;
    private NightModeAsyncImageView h;
    private EllipsisTextView j;
    private TextView k;
    private int l;
    private int m;
    private com.ss.android.article.base.feature.detail2.a.aa n;

    public DetailAdSmallPicLayout(Context arg2) {
        super(arg2);
        this.b = "";
    }

    public DetailAdSmallPicLayout(Context arg2, byte arg3) {
        super(arg2, (byte) 0);
        this.b = "";
    }

    private void a(int arg3, int arg4) {
        ViewGroup.LayoutParams v0 = this.h.getLayoutParams();
        if (v0 != null) {
            if (arg3 > 0) {
                v0.width = arg3;
            }

            if (arg4 > 0) {
                v0.height = arg4;
            }

            this.h.setLayoutParams(v0);
        }
    }

    public final void initView() {
        super.initView();
        if (this.i == 0) {
            this.setBackgroundResource(com.ss.android.article.browser.R.drawable.detail_ad_bg);
        } else if (this.i == 1) {
            this.setBackgroundResource(com.ss.android.article.browser.R.color.transparent);
        }

        this.h = this.findViewById(com.ss.android.article.browser.R.id.ad_pic);
        this.a = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_source);
        this.j = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_title);
        this.k = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_label);
        this.c = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_creative);
        this.c.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.download_ad_details, 0, 0, 0);
    }

    public final void a(final BaseAdsData arg7) {
        BaseAdsData v0_1;
        int v4 = View.GONE;
        Drawable v3 = null;
        if (arg7 != null) {
            this.d = arg7.id;
            try {
                this.g = new JSONObject();
                this.g.put("log_extra", arg7.logExtra);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }

            this.e = arg7.pkg;
            this.n = new com.ss.android.article.base.feature.detail2.a.aa(arg7);
            if ((arg7 instanceof DownloadAppAd)) {
                v0_1 = arg7;
                this.f = new a(this.getContext(), ((DownloadAppAd) v0_1));
                this.c.setVisibility(View.VISIBLE);
                if (!android.support.design.a.isTextEmpty(((DownloadAppAd) v0_1).appAdSourceLabel)) {
                    this.k.setText(((DownloadAppAd) v0_1).appAdSourceLabel);
                }

                TextView v2 = this.c;
                String v1 = android.support.design.a.isTextEmpty(((DownloadAppAd) v0_1).btnTxt) ? this.getResources().getString(com.ss.android.article.browser.R.string.j2) : ((DownloadAppAd) v0_1).btnTxt;
                v2.setText(((CharSequence) v1));
                this.b = ((DownloadAppAd) v0_1).appName;
                this.j.setText(((DownloadAppAd) v0_1).appAdTitle);
                this.a.setText(this.b);
                this.c.setOnClickListener(new View.OnClickListener() {
                    public final void onClick(View arg2) {
                        if (DetailAdSmallPicLayout.this.f != null) {
                            DetailAdSmallPicLayout.this.f.c();
                        }
                    }
                });
                if (((DownloadAppAd) v0_1).imageInfo == null) {
                } else {
                    this.b(((DownloadAppAd) v0_1).imageInfo.mWidth, ((DownloadAppAd) v0_1).imageInfo.mHeight);
                    this.a(this.l, this.m);
                    this.setAdImage(android.support.design.a.imageInfo2Image(((DownloadAppAd) v0_1).imageInfo));
                }
            } else {
                if ((arg7 instanceof MixedAdsData)) {
                    v0_1 = arg7;
                    this.a.setText(((MixedAdsData) v0_1).i);
                    if (!android.support.design.a.isTextEmpty(((MixedAdsData) v0_1).h)) {
                        this.k.setText(((MixedAdsData) v0_1).h);
                    }

                    this.b(((MixedAdsData) v0_1).e, ((MixedAdsData) v0_1).f);
                    this.a(this.l, this.m);
                    this.h.setUrl(((MixedAdsData) v0_1).d);
                    this.j.setText(((MixedAdsData) v0_1).title);
                    this.c.setVisibility(v4);
                } else if ((arg7 instanceof PhoneAdsData)) {
                    v0_1 = arg7;
                    this.c.setVisibility(View.VISIBLE);
                    this.c.setCompoundDrawablesWithIntrinsicBounds(v3, v3, v3, v3);
                    this.a.setText(((PhoneAdsData) v0_1).sourceName);
                    if (!android.support.design.a.isTextEmpty(((PhoneAdsData) v0_1).h)) {
                        this.k.setText(((PhoneAdsData) v0_1).h);
                    }

                    this.b(((PhoneAdsData) v0_1).imageWidth, ((PhoneAdsData) v0_1).imageHeight);
                    this.a(this.l, this.m);
                    this.h.setUrl(((PhoneAdsData) v0_1).image);
                    this.j.setText(((PhoneAdsData) v0_1).title);
                    if (!TextUtils.isEmpty(((PhoneAdsData) v0_1).phoneNumber) && !TextUtils.isEmpty(((PhoneAdsData) v0_1).buttonText)) {
                        this.c.setText(((PhoneAdsData) v0_1).buttonText);
                        this.c.setOnClickListener(new q(this, ((PhoneAdsData) v0_1)));
                    } else {
                        this.c.setVisibility(v4);
                    }
                } else {
                    this.c.setVisibility(v4);
                }
            }
            this.setOnClickListener(new View.OnClickListener() {
                public final void onClick(View arg8) {
                    String v2 = (arg7 instanceof PhoneAdsData) ? "detail_call" : "detail_ad";
                    if (DetailAdSmallPicLayout.this.f != null) {
                        DetailAdSmallPicLayout.this.f.b();
                    } else {
                        AdsAppItem.a(DetailAdSmallPicLayout.this.getContext(), arg7.openUrl, arg7.webUrl,
                                arg7.webTitle, arg7.orientation, new AdsAppItem.b(
                                        DetailAdSmallPicLayout.this.getContext(),
                                        v2, "click",
                                        arg7.id,
                                        arg7.logExtra));
                    }
                }
            });
        }
    }

    public final void a(DownloadShortInfo arg12) {
        int v9 = R.string.detail_appad_smallpic_source_downloading;
        int v8 = R.string.detail_appad_smallpic_download_failed;
        String v1 = "";
        String v0 = this.getContext().getString(v9, new Object[]{Integer.valueOf(0)});
        if (arg12 == null) {
            v0 = this.getContext().getString(com.ss.android.article.browser.R.string.detail_appad_smallpic_start);
            v1 = v0;
            v0 = this.b;
        } else {
            int v2 = ((int) ((((double) arg12.d)) / (((double) arg12.c)) * 100));
            switch (arg12.b) {
                case 1:
                case 2:
                    v1 = this.getContext().getString(R.string.detail_appad_smallpic_pause);
                    v0 = this.getContext().getString(v9, new Object[]{Integer.valueOf(v2)});
                    break;
                case 4:
                    v1 = this.getResources().getString(R.string.detail_appad_smallpic_resume);
                    v0 = this.getContext().getString(R.string.detail_appad_smallpic_source_pausing, new Object[]{Integer.valueOf(v2)});
                    break;
                case 8:
                    if (ToolUtils.isAppInstalled(this.getContext(), this.e)) {
                        v0 = this.getContext().getString(R.string.detail_appad_smallpic_installed);
                        this.n.a("detail_download_ad");
                    } else {
                        v0 = this.getContext().getString(R.string.detail_appad_smallpic_download_finished);
                    }
                    v1 = v0;
                    v0 = this.b;
                    break;
                case 16:
                    v1 = this.getResources().getString(v8);
                    v0 = this.getContext().getString(v8);
                    break;
            }
        }

        this.a.setText(((CharSequence) v0));
        this.c.setText(((CharSequence) v1));
    }

    public final void a(DownloadShortInfo arg10, int arg11, long arg12, long arg14) {
        this.post(new n(this, arg14, arg12, arg11, arg10));
    }

    public final void adaptNightMode(boolean arg6) {
        int v1 = R.color.update_comment_more_text_selector;
        Resources v2 = this.getResources();
        TextView v3 = this.k;
        int v0 = this.i == 0 ? R.color.ssxinzi12 : v1;
        v3.setTextColor(v2.getColorStateList(v0));
        v0 = this.i == 0 ? R.drawable.detail_ad_bg : R.drawable.e;
        this.setBackgroundResource(v0);
        v3 = this.a;
        v0 = this.i == 0 ? R.color.ssxinheihui3 : R.color.ssxinzi3;
        v3.setTextColor(v2.getColorStateList(v0));
        this.j.setTextColor(v2.getColorStateList(com.ss.android.article.browser.R.color.ssxinzi1));
        this.c.setTextColor(v2.getColorStateList(v1));
        if (this.c.getCompoundDrawables()[0] != null) {
            this.c.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.download_ad_details, 0, 0, 0);
        }

        this.h.adaptNightMode(arg6);
    }

    private void b(int arg5, int arg6) {
        if (arg5 != 0 && arg6 != 0) {
            this.l = (this.getResources().getDisplayMetrics().widthPixels - 72) / 3;
            this.m = ((int) ((((double) this.l)) * (((double) arg6)) / (((double) arg5))));
        }
    }

    public final void b() {
        if (this.f != null) {
            this.f.d();
        }
    }

    public final int getLayoutRes() {
        int v0 = this.i == 0 ? R.layout.new_detail_ad_small_pic : R.layout.detail_video_ad_small_pic;
        return v0;
    }

    protected final void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.f != null) {
            this.f.a(((z) this));
        }
    }

    protected final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.f != null) {
            this.f.a();
        }
    }

    public final void setAdImage(Image arg2) {
        this.h.setImage(arg2);
    }
}

